// pages/smartlife/bus/bus.js
var common = require('../../../utils/common.js');
var startTime = null
var endTime = null
var timeId = null
Page({
  data:{
    status:0,
    buslineList: [],
    stationList: [],
    searchList: [],
    buslineIndex:-1,
    stationIndex:-1,
    searchText:'',
    // appInfo: '',
    // adData: null
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    imgWidth: 0,
    imgHeight: 0,
    swipers: []
  },
  onLoad:function(options){
    var that=this
    try {
      var busline = wx.getStorageSync('busline')
      var station = wx.getStorageSync('station')
      // var appUsed = wx.getStorageSync('appused')
      // if (!appUsed) {
      //     // 跳转到app使用引导页面
      //     wx.navigateTo({
      //       url: '/pages/smartlife/guide/guide'
      //     })
      //     wx.setStorageSync('appused', true)
      // }
      
      try {
        var res = wx.getSystemInfoSync()
        that.setData({
          imgWidth: res.windowWidth,
          imgHeight: res.windowWidth * 9 / 16,
          recommendImgWidth: res.windowWidth - 20
        })
        wx.setTopBarText({
          text: '我将改变世界!'
        })
      } catch (e) {
        // Do something when catch error
      }
      that.onPullDownRefresh()
      if (busline) {
          that.setData({
            buslineList:busline
          })
      }
      if (station) {
          that.setData({
            stationList:station
          })
      }
      // wx.request({
      //   url: common.apiUrl() + 'appInfomation',
      //   // header: {}, // 设置请求的 header
      //   success: function (res) {
      //     // console.log(res)
      //     that.setData({
      //       appInfo: res.data.data
      //     })
      //   }
      // })
    } catch (e) {
      // Do something when catch error
    }
  },
  onPullDownRefresh: function () {
    var that = this
    that.getSwipers()
  },
  getSwipers: function (e) {
    var that = this
    // 请求获取swipers列表
    wx.request({
      url: common.apiUrl() + 'swipersList',
      success: function (res) {
        var list = res.data.data
        list.splice(list.length - 1, 1)
        // console.log(list)
        that.setData({
          swipers: list
        })
        wx.stopPullDownRefresh()
      }
    })
  },
  onShareAppMessage: function () {
    return {
      title: '昆山最方便的实时公交系统',
      path: '/pages/smartlife/bus/bus'
    }
  },
  titleChange:function(e){
    var that = this
    if(that.data.status!=e.currentTarget.dataset.status){
      that.setData({
        searchText:'',
        searchList:[]
      })
    }
    that.setData({
      status:e.currentTarget.dataset.status
    })
  },
  searchClick:function(e){
    var that = this
    var index = e.currentTarget.dataset.index
    var list = []
    if(that.data.status==0){
      list = that.data.buslineList
    }else{
      list = that.data.stationList
    }
    console.log(list)
    var item = that.data.searchList[index]
    for(var i=0;i<list.length;i++){
      // console.log(JSON.stringify(item))
      if(JSON.stringify(list[i])==JSON.stringify(item)){
        // console.log('same')
        //去除之前已有记录
        list.splice(i,1)
        break
      }
    }
    //新增刚搜索的记录
    list.unshift(item)
    if(that.data.status==0){
      that.setData({
        buslineList:list
      })
    }else{
      that.setData({
        stationList:list
      })
    }
    
    try {
      if(that.data.status==0){
        wx.setStorageSync('busline', that.data.buslineList)
      }else{
        wx.setStorageSync('station', that.data.stationList)
      }
    } catch (e) {    
    }
    // console.log(that.data.searchList[index])
    if(that.data.status==0){
      wx.navigateTo({
        url: '/pages/smartlife/bus/busline/busline?lineId=' + that.data.searchList[index].lineId + '&lineName=' + that.data.searchList[index].lineName + '&upDown=' + that.data.searchList[index].upOrDown + '&startStationName=' + that.data.searchList[index].fristStation + '&endStationName=' + that.data.searchList[index].lastStation
      })
    }else{
      wx.navigateTo({
        url: '/pages/smartlife/bus/station/station?stationId='+index+'&stationName='+that.data.searchList[index]
      })
    }
    that.setData({
      searchText:'',
        searchList:[]
    })
  },
  locationClick:function(e){
    // console.log(e)
    wx.navigateTo({
      url: '/pages/smartlife/bus/location/location'
    })
  },
  buslineClick:function(e){
    if(this.endTime  - this.startTime < 350) {
      var that = this
      var index = e.currentTarget.dataset.index
      wx.navigateTo({
        url: '/pages/smartlife/bus/busline/busline?lineId=' + that.data.buslineList[index].lineId + '&lineName=' + that.data.buslineList[index].lineName + '&upDown=' + that.data.buslineList[index].upOrDown + '&startStationName=' + that.data.buslineList[index].fristStation + '&endStationName=' + that.data.buslineList[index].lastStation
      })
      var list = []
      list = that.data.buslineList
      var item = that.data.buslineList[index]
      for(var i=0;i<list.length;i++){
        if(JSON.stringify(list[i])==JSON.stringify(item)){
          list.splice(i,1)
          break
        }
      }
      list.unshift(item)
      that.setData({
        buslineList:list
      })
      try {
        wx.setStorageSync('busline', that.data.buslineList)
      } catch (e) {    
      }
    }
  },
  stationClick:function(e){
    if(this.endTime  - this.startTime < 350) {
      var that = this
      var index = e.currentTarget.dataset.index
      wx.navigateTo({
        url: '/pages/smartlife/bus/station/station?stationId='+index+'&stationName='+that.data.stationList[index]
      })
      var list = []
      list = that.data.stationList
      var item = that.data.stationList[index]
      for(var i=0;i<list.length;i++){
        if(JSON.stringify(list[i])==JSON.stringify(item)){
          list.splice(i,1)
          break
        }
      }
      list.unshift(item)
      that.setData({
        stationList:list
      })
      try {
        wx.setStorageSync('station', that.data.stationList)
      } catch (e) {    
      }
    }
  },
  swiperItemClick: function (e) {
    var that = this
    if (isNaN(e.currentTarget.dataset.link)) {
      if (e.currentTarget.dataset.link) {
        if ((e.currentTarget.dataset.link.indexOf('play_list') >= 0) || (e.currentTarget.dataset.link.indexOf('budejie') >= 0)) {//无路由
          wx.navigateToMiniProgram({
            appId: 'wxf407663f1b4e50fd',
            path: e.currentTarget.dataset.link.substring(1),
            success(res) {
              // 打开成功
            }
          })
        }else{//有路由
          var page = e.currentTarget.dataset.link + '?recommendid=' + that.data.swipers[e.currentTarget.dataset.index].id + '&title=' + that.data.swipers[e.currentTarget.dataset.index].title + '&img=' + that.data.swipers[e.currentTarget.dataset.index].img + '&content=' + that.data.swipers[e.currentTarget.dataset.index].content
          // console.log(page)
          wx.navigateTo({
            url: page,
            success: function (res) {
              // success
            },
            fail: function () {
              // fail
            },
            complete: function () {
              // complete
            }
          })
        }
      } else {
        // wx.showToast({
        //   title: '暂未开放'
        // })
      }
    } else {
      if (e.currentTarget.dataset.link == null || e.currentTarget.dataset.link == undefined || e.currentTarget.dataset.link == '') {
        // console.log(e.currentTarget.dataset.link)
      } else {
        wx.makePhoneCall({
          phoneNumber: e.currentTarget.dataset.link,
        })
      }
    }
  },
  buslineTouchStart: function(e) {
    this.startTime = e.timeStamp;
  },
  stationTouchStart: function(e) {
    this.startTime = e.timeStamp;
  },
  buslineTouchEnd: function(e) {
    this.endTime = e.timeStamp;
  },
  stationTouchEnd: function(e) {
    this.endTime = e.timeStamp;
  },
  deleteBusline:function(e){
    var that = this
    that.setData({
      buslineIndex:e.currentTarget.dataset.index
    })

    wx.showActionSheet({
      itemList: ['删除'],
      itemColor: '#E64340',
      success: function(res) {
        switch(res.tapIndex){
          case 0:
            var list = that.data.buslineList
            list.splice(e.currentTarget.dataset.index,1)
            // console.log('删除-----'+e.currentTarget.dataset.index)
            that.setData({
              buslineIndex:-1,
              buslineList:list
            })
            try {
              wx.setStorageSync('busline', list)
            } catch (e) {    
            }
          break;
          case 1:

          break;
        }
      },
      fail: function(res) {
        that.setData({
          buslineIndex:-1
        })
      }
    })
  },
  deleteStation:function(e){
    var that = this
    that.setData({
      stationIndex:e.currentTarget.dataset.index
    })

    wx.showActionSheet({
      itemList: ['删除'],
      itemColor: '#E64340',
      success: function(res) {
        switch(res.tapIndex){
          case 0:
            var list = that.data.stationList
            list.splice(e.currentTarget.dataset.index,1)
            // console.log('删除-----'+e.currentTarget.dataset.index)
            that.setData({
              stationIndex:-1,
              stationList:list
            })
            try {
              wx.setStorageSync('station', list)
            } catch (e) {    
            }
          break;
          case 1:

          break;
        }
      },
      fail: function(res) {
        that.setData({
          stationIndex:-1
        })
      }
    })
  },
  searchInput:function(e){
    var that=this
    clearTimeout(that.timeId)
    if(e.detail.value==''){
      that.setData({
        searchList:[]
      })
      return
    }
    that.timeId = setTimeout(function(){
      if(that.data.status==0){
        // console.log('1秒后请求数据：'+e.detail.value)
        wx.request({
          url: common.busUrl() +'lineSearch/hints',
          data: {
              name:e.detail.value
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          method: "POST",
          success: function (res) {
            if (res.data.success == 1) {
              console.log(res.data.data)
              that.setData({
                searchList:res.data.data
              })
            }else{
                
            }
          }
        })
      }else{
        wx.request({
          url: common.busUrl()+'stationSearch/hints',
          data: {
              name:e.detail.value
          },
          // header: {}, // 设置请求的 header
          success: function(res){
            if (res.data.success == 1) {
              console.log(res)
              that.setData({
                searchList:res.data.data
              })
            }else{
                
            }
          }
        })
      }
    },500)

    
    // console.log(e.detail.value)
      
  },
  gotoXiaoZhi:function (e) {
    wx.navigateToMiniProgram({
      appId: 'wxf407663f1b4e50fd',
      path: '',
      success(res) {
        // 打开成功
      }
    })
  },
  // closeAd: function (e) {
  //   this.setData({
  //     adData: null
  //   })
  // },
  // adLink: function (e) {
  //   var that = this
  //   if (isNaN(that.data.adData.link)) {
  //     if (that.data.adData.link) {
  //       // console.log(page)
  //       wx.navigateTo({
  //         url: that.data.adData.link,
  //         success: function (res) {
  //           // success
  //         },
  //         fail: function () {
  //           // fail
  //         },
  //         complete: function () {
  //           // complete
  //         }
  //       })
  //     } else {
  //       // wx.showToast({
  //       //   title: '暂未开放'
  //       // })
  //     }
  //   } else {
  //     if (that.data.adData.link == null || that.data.adData.link == undefined || that.data.adData.link == '') {
  //     } else {
  //       wx.makePhoneCall({
  //         phoneNumber: that.data.adData.link,
  //       })
  //     }
  //   }
  // }
})